package com.dzl.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.dzl.base.service.BaseServiceImpl;
import com.dzl.config.database.Master;
import com.dzl.express.utils.KuaiIdi100Utils;
import com.dzl.express.utils.Result;
import com.dzl.express.utils.ResultItem;
import com.dzl.order.dao.WelfareOrderExpressMapper;
import com.dzl.order.pojo.WelfareOrderExpress;
import com.dzl.order.pojo.vo.WelfareOrderExpressVo;
import com.dzl.order.service.WelfareOrderExpressService;
import com.dzl.util.DateUtil;
import com.dzl.util.EmptyUtils;
import com.dzl.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 品牌信息表(WelfareOrderExpress)表服务实现类
 * @author caobucao
 * @date 2021-05-28 17:06:08
 */
@Service
public class WelfareOrderExpressServiceImpl extends BaseServiceImpl<WelfareOrderExpress> implements WelfareOrderExpressService {

    @Autowired
    private WelfareOrderExpressMapper mapper;

    @Master
    @Override
    public List<WelfareOrderExpressVo> getOrderExpressInfo(Integer orderId) {
        String key = RedisUtil.EXPRESS_QUERY_LOCK_PREFIX + orderId;

        List<WelfareOrderExpressVo> welfareOrderExpressVoList = mapper.selectOrderExpressByOrderId(orderId);
        if(CollectionUtils.isEmpty(welfareOrderExpressVoList)) {
            return new ArrayList<>();
        }

        if (RedisUtil.get(key) != null) {
            //welfareOrderExpressVoList.forEach(x -> x.setExpressList(JSONArray.parseArray(x.getData(), ResultItem.class)));
            welfareOrderExpressVoList.forEach(x -> {
                if(EmptyUtils.isNotEmpty(x.getData())) {
                    x.setExpressList(JSONArray.parseArray(x.getData(), ResultItem.class));
                } else {
                    List<ResultItem> dataList = new ArrayList<>();
                    if(x.getExpressCode().equals("zixingpeisong") && x.getExpressName().equals("自行配送")) {
                        ResultItem resultItem = new ResultItem();
                        resultItem.setContext(x.getExpressNo());
                        resultItem.setFtime(DateUtil.format2DateTime(x.getCreateTime()));
                        resultItem.setTime(DateUtil.format2DateTime(x.getCreateTime()));
                        dataList.add(resultItem);
                    } else {
                        Result result= KuaiIdi100Utils.getExpressInfo(x.getExpressCode(), x.getExpressNo());
                        if(EmptyUtils.isNotEmpty(result)) {
                            dataList = result.getData();
                        }
                    }
                    x.setExpressList(dataList);
                    //物流信息存入数据库
                    x.setData(JSON.toJSONString(dataList));
                    x.setUpdateTime(new Date());
                    mapper.updateByPrimaryKeySelective(x);
                }
            });
            return welfareOrderExpressVoList;
        }
        // 设置查询间隔 1小时只实时查询一次
        RedisUtil.set(key, "cache", 3600);

        welfareOrderExpressVoList.forEach(x -> {
            List<ResultItem> dataList=new ArrayList<>();
            if(x.getExpressCode().equals("zixingpeisong") && x.getExpressName().equals("自行配送")) {
                ResultItem resultItem = new ResultItem();
                resultItem.setContext(x.getExpressNo());
                resultItem.setFtime(DateUtil.format2DateTime(x.getCreateTime()));
                resultItem.setTime(DateUtil.format2DateTime(x.getCreateTime()));
                dataList.add(resultItem);
            } else {
                Result result= KuaiIdi100Utils.getExpressInfo(x.getExpressCode(), x.getExpressNo());
                if(EmptyUtils.isNotEmpty(result)) {
                    dataList = result.getData();
                }
            }
            x.setExpressList(dataList);
            //物流信息存入数据库
            x.setData(JSON.toJSONString(dataList));
            x.setUpdateTime(new Date());
            mapper.updateByPrimaryKeySelective(x);
        });
        return welfareOrderExpressVoList;
    }

    @Override
    public Integer deleteByOrderId(Integer orderId) {
        return mapper.deleteByOrderId(orderId);
    }
}
